package com.markbuikema.juliana32.fragment;

import java.util.GregorianCalendar;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.markbuikema.juliana32.R;
import com.markbuikema.juliana32.adapter.AgendaWeekPagerAdapter;
import com.markbuikema.juliana32.util.Constants;
import com.markbuikema.juliana32.util.DateTimeUtils;

public class AgendaFragment extends Fragment {

	private ImageButton rightButton;
	private ImageButton leftButton;
	private TextView weekText;
	private ViewPager weekPager;

	private PagerAdapter adapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_agenda, container, false);

		rightButton = (ImageButton) view.findViewById(R.id.agenda_week_next);
		leftButton = (ImageButton) view.findViewById(R.id.agenda_week_previous);
		weekText = (TextView) view.findViewById(R.id.agenda_week_text);
		weekPager = (ViewPager) view.findViewById(R.id.agenda_week_pager);
		weekPager.setOffscreenPageLimit(3);

		adapter = new AgendaWeekPagerAdapter(getActivity());

		weekPager.setAdapter(adapter);

		GregorianCalendar today = new GregorianCalendar();
		int currentWeek = today.get(GregorianCalendar.WEEK_OF_YEAR);
		weekPager.setCurrentItem(currentWeek, false);
		weekText.setText(DateTimeUtils.getWeekDescription(weekPager.getCurrentItem()));

		rightButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (weekPager.getCurrentItem() < Constants.WEEKS_IN_YEAR - 1)
					weekPager.setCurrentItem(weekPager.getCurrentItem() + 1);
			}
		});

		leftButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (weekPager.getCurrentItem() > 0)
					weekPager.setCurrentItem(weekPager.getCurrentItem() - 1);
			}
		});

		weekPager.setOnPageChangeListener(new OnPageChangeListener() {

			private static final float ALPHA_THRESHOLD = 0.01f;
			private int mIndex = weekPager.getCurrentItem();
			private int lastIndex = -1;

			@Override
			public void onPageSelected(int index) {
				weekText.setText(DateTimeUtils.getWeekDescription(index));
				getActivity().invalidateOptionsMenu();

			}

			@Override
			public void onPageScrolled(int index, float ratio, int pixels) {
				// do the alpha animation
				float alpha = 2f * (ratio - .5f);
				weekText.setAlpha(Math.abs(alpha));

				// // If the text hasn't been set and ratio is at least halfway
				// // from 0 -> 1
				// // alpha should be near 0, ideally exactly 0
				// if ((mIndex != lastIndex) && (alpha <= ALPHA_THRESHOLD)) {
				//
				// if (index == mIndex -1)
				// mIndex = index -1;
				// else
				// mIndex = index;
				//
				// weekText.setText(DateTimeUtils.getWeekDescription(mIndex));
				// lastIndex = mIndex;
				// }
			}

			@Override
			public void onPageScrollStateChanged(int state) {
			}
		});

		getActivity().invalidateOptionsMenu();

		return view;
	}

	public void setWeek(int current) {
		weekPager.setCurrentItem(current);
	}

	public int getWeek() {
		return weekPager.getCurrentItem();
	}
}
